package com.njust.service.Impl;

import com.njust.dataobject.User;
import com.njust.dataobject.UserExample;
import com.njust.mapper.UserMapper;
import com.njust.service.UserService;
import com.njust.utils.MD5Util;
import com.njust.vo.PasswordVO;
import com.njust.vo.ResultVO;
import com.njust.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zjwpro@foxmail.com
 * @date 2018/1/12 下午5:44
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public ResultVO findByUsername(String username){
        User u = userMapper.findByUsername(username);
        if(u!=null){
            return ResultVO.createByErrorMessage("没有该用户");
        }
        return ResultVO.createBySuccess(u);
    }

    /**
     * 注册或者添加用户 那么如何知道是添加用户呢？
     * @param user
     * @return
     */
    @Override
    public ResultVO save(User user) {
        User u = userMapper.findByUsername(user.getUsername());
        if(u!=null){
            return ResultVO.createByErrorMessage("用户名已存在，请换一个用户名");
        }
        String encodePass=MD5Util.encode(user.getPassword());
        User newuser=new User();
        BeanUtils.copyProperties(user,newuser);
        newuser.setPassword(encodePass);
        newuser.setCreatetime(new Date());
        int i = userMapper.insertSelective(newuser);
        if(i>0){
            return ResultVO.createBySuccess();
        }
        return ResultVO.createByError();
    }

    /**
     * 更新用户
     * @param user
     * @return
     */
    @Override
    public ResultVO update(User user) {
        if(user.getUsername()==null){
            return ResultVO.createByErrorMessage("用户名不能为空");
        }
        User byUsername = userMapper.findByUsername(user.getUsername());
        if(byUsername==null){
            return ResultVO.createByErrorMessage("不存在该用户名");
        }

        UserExample userExample = new UserExample();
        //UserExample.Criteria criteria = userExample.createCriteria();
        user.setName(byUsername.getName());
        user.setCreatetime(byUsername.getCreatetime());
        user.setUsername(byUsername.getUsername());
        user.setId(byUsername.getId());
        user.setUserrole(byUsername.getUserrole());
        int result = userMapper.updateByPrimaryKeySelective(user);
        if(result>0){
            return ResultVO.createBySuccess("更新成功");
        }
        return ResultVO.createByErrorMessage("更新失败");
    }

    /**
     * 删除用户
     * @param username
     * @return
     */
    @Override
    public ResultVO deleteUser(String username) {
        int result = userMapper.deleteByUsername(username);
        if(result>0){
            return ResultVO.createBySuccess("删除成功");
        }
        return ResultVO.createByErrorMessage("删除失败");
    }

    @Override
    public User login(User user) {
        String password=user.getPassword();
        String username=user.getUsername();
        String MD5password = MD5Util.encode(password);
        User u = userMapper.findByUsername(username);
        if (u.getPassword().equals(MD5password)) {
            return u;
        }
        return null;
    }


    /**
     * 修改密码
     * @param passwordVO
     * @return
     */
    @Override
    public ResultVO modifyPassword(PasswordVO passwordVO) {
        if(passwordVO.getOldPassword()==null){
            return ResultVO.createByErrorMessage("旧密码不能为空");
        }
        if(passwordVO.getNewPassword()==null){
            return ResultVO.createByErrorMessage("新密码不能为空");
        }
        User user = userMapper.findByUsername(passwordVO.getUsername());
        //更新的新用户
        User newuser=new User();
        BeanUtils.copyProperties(user,newuser);
        newuser.setPassword(MD5Util.encode(passwordVO.getNewPassword()));
        //保存
        int i = userMapper.updateByPrimaryKey(newuser);
        if(i>0){
            return ResultVO.createBySuccess("密码修改成功");
        }
        return ResultVO.createByErrorMessage("密码修改失败");
    }

    @Override
    public ResultVO findAllUsers() {
        UserExample userExample = new UserExample();
        List<User> users = userMapper.selectByExample(userExample);
        List<UserVO> userVOS=new ArrayList<>();
        for(User user:users){
            UserVO userVO = new UserVO();
            userVO.setName(user.getName());
            userVO.setUsername(user.getUsername());
            userVO.setUserrole(user.getUserrole());
            userVO.setUserstate(user.getUserstate());
            userVOS.add(userVO);
        }
        return ResultVO.createBySuccess(userVOS);
    }
}
